package com.mtbu.dao.base;
import my.myorm.tool.jdbc.RowMapper;
import my.myorm.tool.jdbc.SQLExecutor;
import my.myorm.tool.jdbc.SQLManager;
import my.myorm.tool.orm.TableBeanMap;
import java.sql.ResultSet;
import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import com.mtbu.bean.BppChannel;
public abstract class  BppChannelDaoBase implements RowMapper{
      private String dsName = "";
      public void setDsName(String dsName){
      		this.dsName=dsName;
      }
      public String getDsName(){
      		return this.dsName;
      }
      private String table;
      public void setTable(String table){
      		this.table = table;
      }
      public String getTable(){
      		if(table==null){
      			table = TableBeanMap.getBeanTable(BppChannel.class);
      		}
      		return table;
      }
	private String toSaveSql(SQLExecutor dbconn,BppChannel bppChannel,String split) throws Exception{
		if(split==null) split="";
		if(split.trim().equals("")) split=getSplit(bppChannel);
		Map<String,Object> dirtyProperties=bppChannel.getDirtyProperties();
		if(dirtyProperties.size() == 0) return "";
		StringBuffer sb=new StringBuffer();
		sb.append("insert into " + getTable() + split + " (");
		String[] props = dirtyProperties.keySet().toArray(new String[0]);
		for(int i = 0; i < props.length; i++) {
		      String prop = props[i];
		      String column = TableBeanMap.getColumnName(getTable(), prop);
		      sb.append(" ").append("`" + column + "`");
		      dbconn.addParam(dirtyProperties.get(prop));
		      if(i < props.length - 1) {
		          sb.append(",");
		      }
				}
		 sb.append(" ) values (");
		 for(int i = 0; i < props.length; i++) {
		    sb.append("?");
		     if(i < props.length - 1) {
		         sb.append(",");
		     }
		 }
		 sb.append(")");
		 //dirtyProperties.clear();
		 return sb.toString();
    }
    private String toUpdateSql(SQLExecutor dbconn,String byProperty,BppChannel bppChannel,String split) throws Exception{
       if(split==null) split="";
      if(split.trim().equals("")) split=getSplit(bppChannel);
       Map<String,Object> dirtyProperties=bppChannel.getDirtyProperties();
       if(dirtyProperties.size() == 0) return "";
        String byColumn = TableBeanMap.getColumnName(bppChannel.getClass(), byProperty);
        if(byColumn == null) throw new Exception("byColumn==null");
        StringBuffer sb=new StringBuffer();
        sb.append("update  " + getTable() + split + " set ");
        String[] props = dirtyProperties.keySet().toArray(new String[0]);
        for(int i = 0; i < props.length; i++) {
            String prop = props[i];
            String column = TableBeanMap.getColumnName(getTable(), prop);
            sb.append(" ").append(column).append("=?");
            dbconn.addParam(dirtyProperties.get(prop));
            if(i < props.length - 1) {
                sb.append(",");
            }
        }
        sb.append(" where " + byColumn + "=?");
        //dirtyProperties.clear();
        return sb.toString();
    }
    private String toDeleteSql(String byProperty,BppChannel bppChannel,String split) throws Exception{
      if(split==null) split="";
      if(split.trim().equals("")) split=getSplit(bppChannel);
        String byColumn=TableBeanMap.getColumnName(bppChannel.getClass(),byProperty);
        if(byColumn==null) throw new Exception("byColumn==null");
        String sql="delete from "+getTable()+split+" where "+byColumn+"=?";
        return sql;
    }
    public void save(BppChannel bppChannel,boolean withoutTransaction,String split) throws Exception{
             if(bppChannel==null) return;
             SQLExecutor dbconn= SQLManager.getSQLExecutor(dsName,withoutTransaction);
              String sql=this.toSaveSql(dbconn,bppChannel,split);
              if(sql.equals("")) return;
            bppChannel.setId((int)dbconn.execute(sql,true));
    }
    public void save(BppChannel bppChannel,boolean withoutTransaction) throws Exception{
            save(bppChannel,withoutTransaction,null);
    }
    public void save(BppChannel bppChannel,String split) throws Exception{
            if(bppChannel==null) return;
             SQLExecutor dbconn= SQLManager.getSQLExecutor(dsName);
              String sql=this.toSaveSql(dbconn,bppChannel,split);
              if(sql.equals("")) return;
            if(bppChannel.getId()==0){
            	bppChannel.setId((int)dbconn.execute(sql,true));
            }else{
           		dbconn.execute(sql); 	
            }  
    }
    public void save(BppChannel bppChannel) throws Exception{
            save(bppChannel,null);
    }
    public int update(BppChannel bppChannel,String split) throws Exception{
           return update(null,bppChannel,split);
    }
    public int update(BppChannel bppChannel) throws Exception{
           return update(bppChannel,null);
    }
     public int update(String byProperty,BppChannel bppChannel,String split) throws Exception{
        if(bppChannel==null) return 0;
        SQLExecutor dbconn= SQLManager.getSQLExecutor(dsName);
        if(byProperty==null||byProperty.equals("")){
            byProperty=getDefaultKeyProperty();
        }
        String sql=this.toUpdateSql(dbconn,byProperty,bppChannel,split);
        if(sql.equals("")) return 0;
        dbconn.addParam(getPropertyValue(bppChannel,byProperty));
        int result= (int)dbconn.execute(sql);
         return result;
    }
    public int update(String byProperty,BppChannel bppChannel) throws Exception{
        return update(byProperty,bppChannel,null);
    }
     public int update(BppChannel bppChannel,boolean withoutTransaction,String split) throws Exception{
            return update(null,bppChannel,withoutTransaction,split);
    }
     public int update(BppChannel bppChannel,boolean withoutTransaction) throws Exception{
            return update(bppChannel,withoutTransaction,null);
    }
    public int update(String byProperty,BppChannel bppChannel,boolean withoutTransaction,String split) throws Exception{
        if(bppChannel==null) return 0;
        SQLExecutor dbconn= SQLManager.getSQLExecutor(dsName,withoutTransaction);
        if(byProperty==null||byProperty.equals("")){
            byProperty=getDefaultKeyProperty();
        }
        String sql=this.toUpdateSql(dbconn,byProperty,bppChannel,split);
        if(sql.equals("")) return 0;
        dbconn.addParam(getPropertyValue(bppChannel,byProperty));
        int result= (int)dbconn.execute(sql);
         return result;
    }
    public int update(String byProperty,BppChannel bppChannel,boolean withoutTransaction) throws Exception{
       return  update(byProperty,bppChannel,withoutTransaction,null);
    }
    public int delete(String byProperty,BppChannel bppChannel,String split) throws Exception{
          if(bppChannel==null) return 0;
        SQLExecutor dbconn= SQLManager.getSQLExecutor(dsName);
        if(byProperty==null||byProperty.equals("")){
            byProperty=getDefaultKeyProperty();
        }
        String sql=this.toDeleteSql(byProperty,bppChannel,split);
        dbconn.addParam(getPropertyValue(bppChannel,byProperty));
        int result=(int)dbconn.execute(sql);
        return result;
    }
    public int delete(String byProperty, Object param, String split) throws Exception {
        if(param == null) return 0;
        SQLExecutor dbconn = SQLManager.getSQLExecutor(dsName);
        if(byProperty == null || byProperty.equals("")) {
            byProperty = getDefaultKeyProperty();
        }
        String sql = "delete from " + getTable() + split + " where " + byProperty + "=?";
        dbconn.addParam(param);
        int result = (int) dbconn.execute(sql);
        return result;
    }
    public int delete(Object param) throws Exception {
        return delete(null, param, null);
    }
    public int delete(String byProperty,BppChannel bppChannel) throws Exception{
          return  delete(byProperty,bppChannel,null);
    }
     public int delete(BppChannel bppChannel,String split) throws Exception{
          return delete(null,bppChannel,split);
    }
    public int delete(BppChannel bppChannel) throws Exception{
          return delete(bppChannel,null);
    }
    public int delete(BppChannel bppChannel,boolean withoutTransaction,String split) throws Exception{
         return delete(null,bppChannel,withoutTransaction,split);
    }
    public int delete(BppChannel bppChannel,boolean withoutTransaction) throws Exception{
         return delete(bppChannel, withoutTransaction,null);
    }
    public int delete(String byProperty,BppChannel bppChannel,boolean withoutTransaction,String split) throws Exception{
        if(bppChannel==null) return 0;
        SQLExecutor dbconn= SQLManager.getSQLExecutor(dsName,withoutTransaction);
        if(byProperty==null||byProperty.equals("")){
            byProperty=getDefaultKeyProperty();
        }
        String sql=this.toDeleteSql(byProperty,bppChannel,split);
        dbconn.addParam(getPropertyValue(bppChannel,byProperty));
        int result=(int)dbconn.execute(sql);
        return result;
    }
    public int delete(String byProperty,BppChannel bppChannel,boolean withoutTransaction) throws Exception{
        return  delete(byProperty,bppChannel,withoutTransaction,null);
    }
     public BppChannel findBppChannel(Object param) throws Exception{
           return findBppChannel(null,param,null);
    }
     public BppChannel findBppChannel(Object param,boolean notNull) throws Exception{
    	 BppChannel result = findBppChannel(null,param,null);    	 
         return result == null ? (notNull ? new BppChannel() : null) : result;
    }
    public BppChannel findBppChannel(String byProperty,Object param,String split) throws Exception{
        if(byProperty==null||byProperty.trim().equals("")) byProperty=getDefaultKeyProperty();
        if(split==null) split="";
        split=split.trim();
        String byColumn = TableBeanMap.getColumnName(BppChannel.class, byProperty);
        String sql="select * from " + getTable() + split + " where " + byColumn + "=?";
        SQLExecutor dbconn= SQLManager.getSQLExecutor(dsName,true);
        dbconn.addParam(param);
        return dbconn.queryForBean(sql,BppChannel.class);
    }
    private String getDefaultKeyProperty(){
           return "id";
    }
    private Object getPropertyValue(BppChannel bppChannel,String propertyName) throws Exception{
    if(propertyName==null||propertyName.equals("")) return null;
           if(propertyName.equalsIgnoreCase("id")){
                return bppChannel.getId();
           }
           if(propertyName.equalsIgnoreCase("title")){
                return bppChannel.getTitle();
           }
           if(propertyName.equalsIgnoreCase("epTitle")){
                return bppChannel.getEpTitle();
           }
           if(propertyName.equalsIgnoreCase("rID")){
                return bppChannel.getRID();
           }
           if(propertyName.equalsIgnoreCase("vT")){
                return bppChannel.getVT();
           }
           if(propertyName.equalsIgnoreCase("cataID")){
                return bppChannel.getCataID();
           }
           if(propertyName.equalsIgnoreCase("pID")){
                return bppChannel.getPID();
           }
           if(propertyName.equalsIgnoreCase("gID")){
                return bppChannel.getGID();
           }
           if(propertyName.equalsIgnoreCase("infoID")){
                return bppChannel.getInfoID();
           }
           if(propertyName.equalsIgnoreCase("fileName")){
                return bppChannel.getFileName();
           }
           if(propertyName.equalsIgnoreCase("duration")){
                return bppChannel.getDuration();
           }
           if(propertyName.equalsIgnoreCase("bitrate")){
                return bppChannel.getBitrate();
           }
           if(propertyName.equalsIgnoreCase("fileSize")){
                return bppChannel.getFileSize();
           }
           if(propertyName.equalsIgnoreCase("weight")){
                return bppChannel.getWeight();
           }
           if(propertyName.equalsIgnoreCase("height")){
                return bppChannel.getHeight();
           }
           if(propertyName.equalsIgnoreCase("createTime")){
                return bppChannel.getCreateTime();
           }
           if(propertyName.equalsIgnoreCase("updateTime")){
                return bppChannel.getUpdateTime();
           }
           if(propertyName.equalsIgnoreCase("status")){
                return bppChannel.getStatus();
           }
           if(propertyName.equalsIgnoreCase("rank")){
                return bppChannel.getRank();
           }
           if(propertyName.equalsIgnoreCase("cnt")){
                return bppChannel.getCnt();
           }
           if(propertyName.equalsIgnoreCase("picUrl")){
                return bppChannel.getPicUrl();
           }
           if(propertyName.equalsIgnoreCase("orderType")){
                return bppChannel.getOrderType();
           }
           if(propertyName.equalsIgnoreCase("headTime")){
                return bppChannel.getHeadTime();
           }
           if(propertyName.equalsIgnoreCase("tailTime")){
                return bppChannel.getTailTime();
           }
           if(propertyName.equalsIgnoreCase("tag")){
                return bppChannel.getTag();
           }
           if(propertyName.equalsIgnoreCase("videoLanguage")){
                return bppChannel.getVideoLanguage();
           }
           if(propertyName.equalsIgnoreCase("videoType")){
                return bppChannel.getVideoType();
           }
           if(propertyName.equalsIgnoreCase("opLog")){
                return bppChannel.getOpLog();
           }
           if(propertyName.equalsIgnoreCase("userName")){
                return bppChannel.getUserName();
           }
           if(propertyName.equalsIgnoreCase("contentType")){
                return bppChannel.getContentType();
           }
           if(propertyName.equalsIgnoreCase("videoStatus")){
                return bppChannel.getVideoStatus();
           }
           if(propertyName.equalsIgnoreCase("seriesID")){
                return bppChannel.getSeriesID();
           }
           if(propertyName.equalsIgnoreCase("mainID")){
                return bppChannel.getMainID();
           }
           if(propertyName.equalsIgnoreCase("caption")){
                return bppChannel.getCaption();
           }
           if(propertyName.equalsIgnoreCase("headStart")){
                return bppChannel.getHeadStart();
           }
           if(propertyName.equalsIgnoreCase("tailEnd")){
                return bppChannel.getTailEnd();
           }
           if(propertyName.equalsIgnoreCase("description")){
                return bppChannel.getDescription();
           }
           if(propertyName.equalsIgnoreCase("ftAll")){
                return bppChannel.getFtAll();
           }
           if(propertyName.equalsIgnoreCase("cDNDel")){
                return bppChannel.getCDNDel();
           }
           if(propertyName.equalsIgnoreCase("cP")){
                return bppChannel.getCP();
           }
           if(propertyName.equalsIgnoreCase("sourceFrom")){
                return bppChannel.getSourceFrom();
           }
           return null;
    }
    public Object mapRow(ResultSet rs, Map<String, String> columns) throws Exception {
        BppChannel bppChannel=new BppChannel();
         Map<String,Object> valueMap=new HashMap<String,Object>();
         String[] lables=columns.keySet().toArray(new String[0]); 
         String table=getTable();
         for(int i=0;i < lables.length;i++){
              String propertyName= TableBeanMap.getPropertName(table,columns.get(lables[i]));
              Object value=rs.getObject(lables[i]);
              if(value==null) continue;
              if(value instanceof java.sql.Date){
                          java.sql.Date o=(java.sql.Date)value;
                          value=new Date(o.getTime());
              }else if(value instanceof java.sql.Time){
                          java.sql.Time o=(java.sql.Time)value;
                          value=new Date(o.getTime());
              }else if(value instanceof java.sql.Timestamp){
                          java.sql.Timestamp o=(java.sql.Timestamp)value;
                          value=new Date(o.getTime());
               }else if(value instanceof java.math.BigDecimal){
                          value=((java.math.BigDecimal)value).doubleValue();
               }else if(value instanceof java.math.BigInteger){
                          value=((java.math.BigInteger)value).longValue();
               }
               valueMap.put(propertyName,value);
         }
         loadFromMap(valueMap,bppChannel);
         valueMap=null;
         return bppChannel;
    }
    private void loadFromMap(Map<String,Object> valueMap,BppChannel bppChannel) throws Exception{
        if(valueMap.containsKey("id")){
             bppChannel.setId((java.lang.Integer)valueMap.get("id"));
        }
        if(valueMap.containsKey("title")){
             bppChannel.setTitle((java.lang.String)valueMap.get("title"));
        }
        if(valueMap.containsKey("epTitle")){
             bppChannel.setEpTitle((java.lang.String)valueMap.get("epTitle"));
        }
        if(valueMap.containsKey("rID")){
             bppChannel.setRID((java.lang.String)valueMap.get("rID"));
        }
        if(valueMap.containsKey("vT")){
             bppChannel.setVT((java.lang.Integer)valueMap.get("vT"));
        }
        if(valueMap.containsKey("cataID")){
             bppChannel.setCataID((java.lang.Integer)valueMap.get("cataID"));
        }
        if(valueMap.containsKey("pID")){
             bppChannel.setPID((java.lang.Integer)valueMap.get("pID"));
        }
        if(valueMap.containsKey("gID")){
             bppChannel.setGID((java.lang.Integer)valueMap.get("gID"));
        }
        if(valueMap.containsKey("infoID")){
             bppChannel.setInfoID((java.lang.Integer)valueMap.get("infoID"));
        }
        if(valueMap.containsKey("fileName")){
             bppChannel.setFileName((java.lang.String)valueMap.get("fileName"));
        }
        if(valueMap.containsKey("duration")){
             bppChannel.setDuration((java.lang.Integer)valueMap.get("duration"));
        }
        if(valueMap.containsKey("bitrate")){
             bppChannel.setBitrate((java.lang.Integer)valueMap.get("bitrate"));
        }
        if(valueMap.containsKey("fileSize")){
             bppChannel.setFileSize((java.lang.Long)valueMap.get("fileSize"));
        }
        if(valueMap.containsKey("weight")){
             bppChannel.setWeight((java.lang.Integer)valueMap.get("weight"));
        }
        if(valueMap.containsKey("height")){
             bppChannel.setHeight((java.lang.Integer)valueMap.get("height"));
        }
        if(valueMap.containsKey("createTime")){
             bppChannel.setCreateTime((java.util.Date)valueMap.get("createTime"));
        }
        if(valueMap.containsKey("updateTime")){
             bppChannel.setUpdateTime((java.util.Date)valueMap.get("updateTime"));
        }
        if(valueMap.containsKey("status")){
             bppChannel.setStatus((java.lang.Integer)valueMap.get("status"));
        }
        if(valueMap.containsKey("rank")){
             bppChannel.setRank((java.lang.Integer)valueMap.get("rank"));
        }
        if(valueMap.containsKey("cnt")){
             bppChannel.setCnt((java.lang.Integer)valueMap.get("cnt"));
        }
        if(valueMap.containsKey("picUrl")){
             bppChannel.setPicUrl((java.lang.String)valueMap.get("picUrl"));
        }
        if(valueMap.containsKey("orderType")){
             bppChannel.setOrderType((java.lang.Integer)valueMap.get("orderType"));
        }
        if(valueMap.containsKey("headTime")){
             bppChannel.setHeadTime((java.lang.Integer)valueMap.get("headTime"));
        }
        if(valueMap.containsKey("tailTime")){
             bppChannel.setTailTime((java.lang.Integer)valueMap.get("tailTime"));
        }
        if(valueMap.containsKey("tag")){
             bppChannel.setTag((java.lang.String)valueMap.get("tag"));
        }
        if(valueMap.containsKey("videoLanguage")){
             bppChannel.setVideoLanguage((java.lang.Integer)valueMap.get("videoLanguage"));
        }
        if(valueMap.containsKey("videoType")){
             bppChannel.setVideoType((java.lang.Integer)valueMap.get("videoType"));
        }
        if(valueMap.containsKey("opLog")){
             bppChannel.setOpLog((java.lang.String)valueMap.get("opLog"));
        }
        if(valueMap.containsKey("userName")){
             bppChannel.setUserName((java.lang.String)valueMap.get("userName"));
        }
        if(valueMap.containsKey("contentType")){
             bppChannel.setContentType((java.lang.Integer)valueMap.get("contentType"));
        }
        if(valueMap.containsKey("videoStatus")){
             bppChannel.setVideoStatus((java.lang.Integer)valueMap.get("videoStatus"));
        }
        if(valueMap.containsKey("seriesID")){
             bppChannel.setSeriesID((java.lang.Integer)valueMap.get("seriesID"));
        }
        if(valueMap.containsKey("mainID")){
             bppChannel.setMainID((java.lang.Integer)valueMap.get("mainID"));
        }
        if(valueMap.containsKey("caption")){
             bppChannel.setCaption((java.lang.Integer)valueMap.get("caption"));
        }
        if(valueMap.containsKey("headStart")){
             bppChannel.setHeadStart((java.lang.Integer)valueMap.get("headStart"));
        }
        if(valueMap.containsKey("tailEnd")){
             bppChannel.setTailEnd((java.lang.Integer)valueMap.get("tailEnd"));
        }
        if(valueMap.containsKey("description")){
             bppChannel.setDescription((java.lang.String)valueMap.get("description"));
        }
        if(valueMap.containsKey("ftAll")){
             bppChannel.setFtAll((java.lang.Integer)valueMap.get("ftAll"));
        }
        if(valueMap.containsKey("cDNDel")){
             bppChannel.setCDNDel((java.lang.Integer)valueMap.get("cDNDel"));
        }
        if(valueMap.containsKey("cP")){
             bppChannel.setCP((java.lang.Integer)valueMap.get("cP"));
        }
        if(valueMap.containsKey("sourceFrom")){
             bppChannel.setSourceFrom((java.lang.Integer)valueMap.get("sourceFrom"));
        }
    }
    public JSONObject toJSON(BppChannel bppChannel)  throws Exception{
        if(bppChannel==null) return null;
        return bppChannel.toJSON();
    }
    public BppChannel fromJSON(JSONObject json) throws Exception{
       if(json==null) return null;
       BppChannel bppChannel=new  BppChannel();
        bppChannel.fromJSON(json);
        return bppChannel;
    }
   public BppChannel fromMap(Map<String,String> paramMap) throws Exception{
       BppChannel bppChannel=new  BppChannel();
       if(paramMap!=null){
           Map<String,String> beanmap=new HashMap<String,String>();
           Iterator it = paramMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                String key = (String) entry.getKey();
                String value =(String)entry.getValue();
                if(key==null) continue;
                int dotindex=key.indexOf(".");
                if(dotindex>-1){
                    String beanname=key.substring(0,dotindex);
                    String propname=key.substring(dotindex+1);
                    if(beanname.equalsIgnoreCase("BppChannel")){
                        beanmap.put(propname.toLowerCase(),value);
                    }
                }else{
                    beanmap.put(key.toLowerCase(),value);
                }
            }
           bppChannel.fromMap(beanmap);
       }
        return bppChannel;
    }
    public abstract String getSplit(BppChannel bppChannel) throws Exception;
}
